import unittest,json
from functools import wraps

from response_class import RequestsEdit


def skip_dependon(depend=""):
    """
    :param depend: 依赖的用例函数名，默认为空
    :return: wraper_func
    """

    def wraper_func(test_func):
        @wraps(test_func)  # @wraps：避免被装饰函数自身的信息丢失
        def inner_func(self):
            if depend == test_func.__name__:
                raise ValueError("{} cannot depend on itself".format(depend))
            # print("self._outcome", self._outcome.__dict__)
            # 此方法适用于python3.4 +
            # 如果是低版本的python3，请将self._outcome.result修改为self._outcomeForDoCleanups
            # 如果你是python2版本，请将self._outcome.result修改为self._resultForDoCleanups
            failures = str([fail[0] for fail in self._outcome.result.failures])
            errors = str([error[0] for error in self._outcome.result.errors])
            skipped = str([skip[0] for skip in self._outcome.result.skipped])
            flag = (depend in failures) or (depend in errors) or (depend in skipped)
            if failures.find(depend) != -1:
                # 输出结果 [<__main__.TestDemo testMethod=test_login>]
                # 如果依赖的用例名在failures中，则判定为失败，以下两种情况同理
                # find()方法：查找子字符串，若找到返回从0开始的下标值，若找不到返回 - 1
                test = unittest.skipIf(flag, "{} failed".format(depend))(test_func)
            elif errors.find(depend) != -1:
                test = unittest.skipIf(flag, "{} error".format(depend))(test_func)
            elif skipped.find(depend) != -1:
                test = unittest.skipIf(flag, "{} skipped".format(depend))(test_func)
            else:
                test = test_func
            return test(self)

        return inner_func

    return wraper_func

def error_ret(traceId, response):
    """错误信息"""
    error_str = "\n traceId: %s; \n response：%s" % (traceId, response)
    return error_str

def error_log(alias, userName, traceId,orderNo, step, response, e):
    '''异常返回接口'''
    error_log = {"alias": alias,
                 "userName": userName,
                 "traceId": traceId,
                 "orderNo": orderNo,
                 "step": step,
                 "response": response,
                 "Except": e
                 }
    postParam = {
        "botkey": "836578d8-0b23-4421-b7e1-277ee3a33055",
        "card": {
            "config": {
                "wide_screen_mode": True,
                "enable_forward": True
            },
            "header": {
                "title": {
                    "content": "【%s%s】买样失败" % (userName, alias),
                    "tag": "plain_text"
                },
                "template": "blue"
            },
            "elements": [
                {
                    "tag": "markdown",
                    "content": "%s" % error_log
                }
            ]
        }
    }

    url = "http://gw.guang.com/guang-app-util/api/feishu/notifyToGroup"
    res = RequestsEdit.postWithoutProxy(postParam, url)
    print(res)
    return res

# def msd_login(mobile):
#     """买手店登录返回headers"""
#     post_data = {
#         "countryCode": "+86",
#         "deviceId": "C8444AD0-2535-408F-BBCD-482EEB3745F6",
#         "mobile": mobile,
#         "password": "JD7hhLGx9/DGKeIoubQCVA==",
#         "temporaryCertificate": "142775-d148e55357eb46feac50b57c87a43df7",
#         "deviceType": "iPhone"
#     }
#     url = 'https://gw.guang.com/v4/guangApp/login/api/loginByPwd'
#     login_ret = RequestsEdit.post(url=url, post_data=post_data).json()
#     yzClientSessionId = login_ret['data']['yzClientSessionId']
#     guangSessionId = login_ret['data']['guangSessionId']
#     Extra = {"appId": "wx367bdbd1f1edeced", "version": "0.2.3", "is_weapp": 1, "sid": yzClientSessionId,
#              "guangSid": guangSessionId}
#     Extra = json.dumps(Extra)
#     headers = {
#         "Extra-Data": Extra,
#         "content-type": "application/json",
#         "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_6_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 Mi croMessenger/8.0.7(0x18000733) NetType/WIFI Language/zh_CN",
#         "Referer": "https://servicewechat.com/wxa2e624400134d690/715/page-frame.html"
#     }
#     return headers
